package cn.jjxx.modules.iface.controller;

import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.modules.pb.entity.PbCheck;
import cn.jjxx.modules.pb.entity.PbEducation;
import cn.jjxx.modules.pb.entity.PbPartyFee;
import cn.jjxx.modules.pb.entity.PbPoliticalLife;
import cn.jjxx.modules.pb.service.*;
import cn.jjxx.modules.record.service.IDevelopmentOfMembersService;
import cn.jjxx.modules.record.service.IPartyOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/iface/road-bridge-party-build")
public class RoadBridgePartyBuildController {

    @Autowired
    private IPbAgeService ageService;
    @Autowired
    private IPartyOrganizationService partyOrganizationService;
    @Autowired
    private IPbEducationService educationService;
    @Autowired
    private IPbSexRateService sexRateService;
    @Autowired
    private IDevelopmentOfMembersService developmentOfMembersService;
    @Autowired
    private IPbPoliticalLifeService politicalLifeService;
    @Autowired
    private IPbPartyFeeService partyFeeService;
    @Autowired
    private IPbCheckService checkService;

    @RequestMapping(value = "org-structure")
    public AjaxJson getOrgStructure(){
        AjaxJson json = new AjaxJson();
        Map<String,Object> data = new HashMap<>();

        List<Map<String,Object>> sqlResultList;
        Map<String,Object> sqlResult;
        sqlResultList = ageService.selectMaps(null);
        EntityWrapper wrapper;
        for(Map<String,Object> map :sqlResultList){
            map.remove("id");
        }
        //原接口的结构如此
        List<Map<String,Integer>> ageStruct = new ArrayList<>();
        Map<String,Integer> ageMap = new HashMap<>();
        for(Map<String,Object> map :sqlResultList){
            ageMap.put(map.get("label").toString(),(Integer) map.get("amount"));
        }
        ageStruct.add(ageMap);
        data.put("ageGroup",ageStruct);

        data.put("djOrgTypeCount",partyOrganizationService.selectOne(null).toJsonObject());

        wrapper = new EntityWrapper<PbEducation>();
        wrapper.orderBy("sort");
        sqlResultList = educationService.selectMaps(wrapper);
        for(Map<String,Object> map :sqlResultList){
            map.remove("id");
            map.remove("sort");
        }
        data.put("eduGroup",sqlResultList);

        sqlResult = sexRateService.selectMap(null);
        sqlResult.remove("id");
        data.put("sexGroup",sqlResult);

        data.put("stageGroup",developmentOfMembersService.selectOne(null).toJsonObject());

        wrapper = new EntityWrapper<PbPoliticalLife>();
        wrapper.orderBy("month");
        sqlResultList = politicalLifeService.selectMaps(wrapper);
        for(Map<String,Object> map :sqlResultList){
            map.remove("id");
        }
        data.put("zzshGroup",sqlResultList);
        json.setData(data);
        json.success("success");
        return json;
    }

    @RequestMapping(value = "party-fee")
    public AjaxJson getPartyFee(){
        AjaxJson json = new AjaxJson();
        EntityWrapper<PbPartyFee> wrapper = new EntityWrapper<>();
        wrapper.orderBy("month+0");
        List<Map<String,Object>> feeList = partyFeeService.selectMaps(wrapper);
        if(feeList==null||feeList.size()==0){
            json.success("success");
            return json;
        }
        for(Map<String,Object> map :feeList){
            map.remove("id");
        }
        Map<String,Object> data = new HashMap<>();
        data.put("sumOfYear",(int)Float.parseFloat(feeList.get(0).get("sumOfYear").toString()));
        for(Map<String,Object> map :feeList){
            map.remove("sumOfYear");
        }
        data.put("monthCount",feeList);
        json.setData(data);
        json.success("success");
        return json;
    }


    @RequestMapping(value = "check")
    public AjaxJson getCheck(){
        AjaxJson json = new AjaxJson();
        EntityWrapper<PbCheck> wrapper = new EntityWrapper<>();
        wrapper.eq("type","aux");
        List<Map<String,Object>> aux = checkService.selectMaps(wrapper);
        for(Map<String,Object> map :aux){
            map.remove("id");
            map.remove("type");
        }
        wrapper = new EntityWrapper<>();
        wrapper.eq("type","eng");
        List<Map<String,Object>> eng = checkService.selectMaps(wrapper);
        for(Map<String,Object> map :eng){
            map.remove("id");
            map.remove("type");
        }
        Map<String,Object> data = new HashMap<>();
        data.put("aux",aux);
        data.put("eng",eng);
        json.setData(data);

        json.success("success");
        return json;
    }
}
